﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Agent
{
    public class Vars
    {
        public static string sipUser = "";
        public static string sipPassword = "";
        public static string sipServerUri = "";
        public static string sipProxyUri = "";
        public static int expiry = 120;
        public static string sipSignUpUrl = "";
        public static SIP? sip;
        public static string message = "";      //报警辅助信息
        public static string chatMessage = "";  //聊天信息
        public static Agent agent = Application.OpenForms.OfType<Agent>().FirstOrDefault();

        public static void Init()
        {
            ReadConfig();
            sip = new SIP();
            sip.StartUp(sipUser, sipPassword, sipServerUri, sipProxyUri, expiry, sipSignUpUrl);

            sip.OnRegisterSuccessEvent += () =>
            {
                if (!sip.isListen)
                {
                    Thread thread = new Thread(new ThreadStart(() =>
                    {
                        sip.Listen();
                    }));
                    thread.IsBackground = true;
                    thread.Start();
                }
                if (!sip.isReceiveMsg)
                {
                    Thread thread2 = new Thread(new ThreadStart(() =>
                    {
                        sip.ReceiveMsg();
                    }));
                    thread2.IsBackground = true;
                    thread2.Start();
                }
            };

            sip.Register();

            sip.OnPassiveRingingEvent += () =>{
                agent.ring();
            };        
            sip.OnPassiveTalkingEvent += () => {
                agent.talk();
            };
            sip.OnEndEvent += () =>
            {
                agent.end();
            };
            sip.OnGetMsgFromInviteEvent += (msg) => {
                agent.getMsgFromInvite(msg);
            };
            sip.OnReceiveMsgEvent += (sipRequest) => {
                agent.receiveMsg(sipRequest);
            };
        }

        public static void ReadConfig()
        {
            string path = Application.StartupPath + "\\config.txt";
            string[] lines = System.IO.File.ReadAllLines(path);
            for (int i = 0; i < lines.Length; i++)
            {
                string[] items = lines[i].Split('=');
                switch (items[0])
                {
                    case "sipUser":
                        sipUser = items[1];
                        break;
                    case "sipPassword":
                        sipPassword = items[1];
                        break;
                    case "sipServerUri":
                        sipServerUri = lines[i].Substring("sipServerUri=".Length);
                        break;
                    case "sipProxyUri":
                        sipProxyUri = lines[i].Substring("sipProxyUri=".Length);
                        break;
                    case "expiry":
                        expiry = int.Parse(items[1]);
                        break;
                    case "sipSignUpUrl":
                        sipSignUpUrl = items[1];
                        break;
                    default:
                        break;
                }
            }
        }
    }
}
